/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn.imgs;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

/**
 * <p>Project: javaseapp - Img1
 * <p>Powered by webrx On 2021-08-12 16:57:08
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 16
 */
public class Img1 {
    public static void main(String[] args) {
        int width = 500;
        int height = 380;
        //new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB) png 图像是画布透明，
        //new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB) 画布不透明，默认是黑色的
        var img = new BufferedImage(width, height, 2);
        Graphics2D g = img.createGraphics();

        //设置图像中文本，线条的平滑
        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
        g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_DEFAULT);

        //画空心没有填充的矩形
        g.setColor(Color.PINK);

        //空心没有填充颜色的矩形
        g.drawRect(0, 0, --width, --height);

        //画直线
        g.setColor(Color.GREEN);
        g.setStroke(new BasicStroke(3.5f));
        g.drawLine(0, 0, width, height);

        g.drawLine(width, 0, 0, height);


        //小作业,要求画出的所有圆形必须随机的在画布中
        //随机画椭圆
        Random rand = new Random();
        for (int i = 0; i <= 10; i++) {
            Color c = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
            g.setColor(c);
            int x = rand.nextInt(width);
            int y = rand.nextInt(height);
            int size = rand.nextInt(50) + 20;
            //小作业,要求画出的所有圆形必须随机的在画布中
            if (x > (width - size)) x = width - size;
            if (y > (height - size)) y = height - size;
            g.fillOval(x, y, size, size);
        }

        //文件格式  gif png jpg
        try {
            ImageIO.write(img, "png", new File("user.png"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        //直接打开user.png
        try {
            Runtime run = Runtime.getRuntime();
            run.exec("cmd /k start user.png");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
